<?php

class User_RightController extends Pldkp_Controller_Action
{
    public function indexAction()
    {
        $rights = User_Model_Rights::getAll();
        $this->view->rights = $rights;
    }
    
    /*
    public function addResourceAction()
    {
        $right = User_Model_Rights::getById($this->_request->getParam('right'));
        
        if (!$right) {
            $this->setFlashMessage('Invalid right!');
            $this->redirect(array('action' => 'index'));
        }
        
        
    }*/
    
    public function resourcesAction()
    {
        $right = User_Model_Rights::getById($this->_request->getParam('right'));
        
        if (!$right) {
            $this->setFlashMessage('Invalid right!');
            $this->redirect(array('action' => 'index'));
        }
        
        $resources = $right->findDependentRowset('User_Model_Resources');
        $this->view->resources = $resources;
        $this->view->right = $right;
    }
    
    
    public function editResourceAction()
    {
        $resource = User_Model_Resources::getById($this->_request->getParam('resource'));
        
        if (!$resource) {
            $this->setFlashMessage('Invalid right!');
            $this->redirect(array('action' => 'index'));
        }
        
        $right = $resource->findParentRow('User_Model_Rights');
        
        $form = new User_Form_ResourceForm($resource);
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                
                $resource->module = $form->getValue('module');
                $resource->controller = $form->getValue('controller');
                $resource->action = $form->getValue('action');
                
                if ($resource->save()) {
                    $this->setFlashMessage('Resource saved!');
                    $this->redirect(array('action' => 'resources', 'right' => $right->id));
                } else {
                    $this->setInstantFlashMessage('Could not save the resource!');
                    $form->populate($postData);
                }
                
            } else {
                $form->populate($postData);
            }
        } else {
            $form->populate(array('module' => $resource->module, 
                                  'controller' => $resource->controller,
                                  'action' => $resource->action));
        }
        
        $this->view->right = $right;
        $this->view->resource = $resource;
        $this->view->form = $form;
    }
    
    public function createResourceAction()
    {
        $right = User_Model_Rights::getById($this->_request->getParam('right'));
        
        if (!$right) {
            $this->setFlashMessage('Invalid right!');
            $this->redirect(array('action' => 'index'));
        } 
        
        $form = new User_Form_ResourceForm();
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                
                $resource = User_Model_Resources::getNew(); 
                $resource->right_id = $right->id;             
                $resource->module = $form->getValue('module');
                $resource->controller = $form->getValue('controller');
                $resource->action = $form->getValue('action');
                
                if ($resource->save()) {
                    $this->setFlashMessage('Resource created!');
                    $this->redirect(array('action' => 'resources', 'right' => $right->id));
                } else {
                    $this->setInstantFlashMessage('Could not save the right!');
                    $form->populate($postData);
                }
            } else {
                $form->populate($postData);
            }
        }
        
        $this->view->right = $right;
        $this->view->form = $form;  
    }
    
   public function deleteResourceAction()
    {
        $resource = User_Model_Resources::getById($this->_request->getParam('resource'));
        
        if (!$resource) {
            $this->setFlashMessage('Invalid resource!');
            $this->redirect(array('action' => 'index'));
        } 
        
        $form = new Pldkp_Form_Confirm();
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            $form->populate($this->_request->getPost());
            if ($form->getValue('yes')) {
                $rightId = $resource->right_id;
                $resource->delete();
                $this->setFlashMessage('Resource deleted!');
                $this->redirect(array('action' => 'resources', 'right' => $rightId ));
                return;
            }
            
            $this->redirect(array('action' => 'edit-resource', 'resource' => $resource->id ));
        }
    }
    
    public function deleteAction()
    {
        $right = User_Model_Rights::getById($this->_request->getParam('right'));
        
        if (!$right) {
            $this->setFlashMessage('Invalid right!');
            $this->redirect(array('action' => 'index'));
        } 
        
        $form = new Pldkp_Form_Confirm();
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            $form->populate($this->_request->getPost());
            if ($form->getValue('yes')) {
                $right->delete();
                $this->setFlashMessage('Right deleted!');
                $this->redirect(array('action' => 'index'));
                return;
            }
            
            $this->redirect(array('action' => 'edit', 'right' => $right->id ));
        }
    }
    
    public function createAction()
    {
        $form = new User_Form_RightForm();
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                
                $right = User_Model_Rights::getNew();              
                $right->name = $form->getValue('name');
                $right->description = $form->getValue('description');
                
                if ($right->save()) {
                    $this->setFlashMessage('Right created!');
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->setInstantFlashMessage('Could not save the right!');
                    $form->populate($postData);
                }
            } else {
                $form->populate($postData);
            }
        }
        
        $this->view->form = $form;  
    }
    
    public function editAction()
    {
        $right = null;
        $right = User_Model_Rights::getById($this->_request->getParam('right'));
        
        if (!$right) {
            $this->setFlashMessage('Invalid right!');
            $this->redirect(array('action' => 'index'));
        } 
        
        $form = new User_Form_RightForm($right);
        $form->submit->setLabel('Edit');
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                
                $right->name = $form->getValue('name');
                $right->description = $form->getValue('description');
                
                if ($right->save()) {
                    $this->setFlashMessage('Right edited!');
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->setInstantFlashMessage('Could not save the right!');
                    $form->populate($postData);
                }
            } else {
                $form->populate($postData);
            }
        } else {
            $form->populate(array('name' => $right->name, 
                                  'description' => $right->description));
        }
        
        $this->view->right = $right;
        $this->view->form = $form;
    }
    
}